import { _decorator, CircleCollider2D, Component, Contact2DType, Director, director, Node, ParticleSystem2D } from 'cc';
import { gameMgr } from './gameMgr';
const { ccclass, property } = _decorator;

@ccclass('Fruit')
export class Fruit extends Component {

    isDie = false;
    private isFallRound = false;

    get IsFallRound() {
        return this.isFallRound;
    }
    set IsFallRound(value) {
        if (this.isFallRound == value) {
            return;
        }
        this.isFallRound = value;
        if (value) {
            // gameMgr.ins.judgeIsBeAboutToGameOverAndDisplay();
            // gameMgr.ins.judgeIsGameOver();
        }
    }

    start() {
        this.node.getComponent(CircleCollider2D).on(Contact2DType.BEGIN_CONTACT, this.benginContactFunc, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    benginContactFunc(self, other, contact) {
        director.once(Director.EVENT_AFTER_PHYSICS, () => {
            if (self.tag!=11) {
                this.IsFallRound=true;
            }
            if (self.tag == 99 && other.tag == 99) {
                return;
            }
            if (self.tag == 99 || other.tag == 99) {//如果是万能球

                if (self.tag < 99 || other.tag < 99) {//如果万能球碰到的是地面直接return
                    return
                }
                gameMgr.ins.isNeedCompoundPos = self.node.position;
                let compondTag;
                console.log(other.tag, "other.tag")
                if (other.tag != 99) {
                    if ((other.tag + 1 - 100) > 11) {
                    gameMgr.ins.isNeedCompound =-1

                        return
                    }
                    else {
                        compondTag = other.tag + 1 - 100;
                    }
                }
                else {
                    if ((self.tag + 1 - 100) > 11) {
                    gameMgr.ins.isNeedCompound =-1

                        return
                    }
                    else {
                        compondTag = self.tag + 1 - 100;
                    }
                }

                console.log(compondTag, "compondtag")
                gameMgr.ins.isNeedCompound = compondTag;

                other.node.destroy();
                self.node.destroy();
            }
            if (other.tag == self.tag) {
                gameMgr.ins.isNeedCompoundPos = self.node.position;
                let compondTag;
                if ((self.tag + 1 - 100) > 11) {
                    gameMgr.ins.isNeedCompound =-1
               
                    return
                }
                else {
                    compondTag = self.tag + 1 - 100;
                }
                gameMgr.ins.isNeedCompound = compondTag;

                other.node.destroy();
                self.node.destroy();
            }
        }, this)

    }

    onTouchEnd(){
        
        if (this.isDie==true) {
            return;
        }
        if (gameMgr.ins.isPowerHammerIng) {
            if (gameMgr.ins.existFruit == this.node || this.node.getComponent(CircleCollider2D).tag == 99) {
                console.log("是万能球不能锤返回")
                return
            }
            else {
                this.die();
                // setTimeout(() => {
                    gameMgr.ins.isPowerHammerIng = false;
                // }, 1000)

                for (let i = 0; i < gameMgr.ins.fruitRoot.children.length; i++) {
                    if (gameMgr.ins.fruitRoot.children[i].getChildByName("hammerSp")) {
                        gameMgr.ins.fruitRoot.children[i].getChildByName("hammerSp").active = false;
                    }
                }
            }
        }
        else {
            return;
        }
    }

    die(){
        this.isDie=true;
        this.node.getChildByName("bombParticle2D").getComponent(ParticleSystem2D).stopSystem();
        this.node.getChildByName("bombParticle2D").getComponent(ParticleSystem2D).resetSystem();
        setTimeout(()=>{
            this.node.destroy();
        },500)
      

    }

}


